package com.situ.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.dao.IvIninvoiceDao;
import com.situ.dao.IvOutinvoiceDao;
import com.situ.entity.IvIninvoice;
import com.situ.service.IvIninvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.situ.service.IvOutinvoiceService;
import com.situ.entity.IvOutinvoice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 * IvOutinvoice的路由接口服务
 *
 * @author 胡文
 */
@Controller
public class IvOutinvoiceController {

    /**
     * IvOutinvoiceService服务
     */
    @Autowired
    private IvOutinvoiceService ivOutinvoiceService;
    private IvIninvoiceService ivIninvoiceService;
    private IvIninvoice StartNo;
    private IvIninvoice EndNo;
    @Autowired(required = false)
    private IvIninvoiceDao ivIninvoiceDao;
    @Autowired(required = false)
    private IvOutinvoiceDao ivOutinvoiceDao;

    /**
     * 查询所有IvOutinvoice数据的方法
     *
     * @param
     * @return
     */
    @RequestMapping("/queryOut")
    public String find(@RequestParam(defaultValue = "1", required = false) int startPage,
                       @RequestParam(defaultValue = "5", required = false) int PageSize,
                       Map map) {
        PageSize = 5;
        PageHelper.startPage(startPage, PageSize);
        List<Map> list = ivOutinvoiceService.find();
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("pageInfo", pageInfo);
        map.put("startPage", startPage);
        map.put("PageSize", PageSize);
        map.put("list", list);
        return "inv_invoiceReportOut.html";
    }

    @RequestMapping("queryIvOutinvoice")
    @ResponseBody
    private void out(IvOutinvoice ivout, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String startno = request.getParameter("startNo");
        String endno = request.getParameter("endNo");
        IvIninvoice w = ivIninvoiceDao.out(startno, endno);
        IvOutinvoice y = ivIninvoiceDao.type(startno, endno);
        PrintWriter out = response.getWriter();
        if (w == null) {
            out.write("true");
        } else {
            Integer iv = y.getIvIninvoice().getInvoiceType();

            if (iv == 0) {
                out.write("普通发票");
            }

            if (iv == 1) {
                out.write("普通发票");
            }
            if (iv == 2) {

                out.write("增值税发票");
            }
            System.out.println("false");
        }
    }

    @RequestMapping("add")
    @ResponseBody
    private void add(IvOutinvoice ivout, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String startno = request.getParameter("StartNo");
        String endno = request.getParameter("EndNo");
        String EmpID = request.getParameter("EmpID");
        IvIninvoice w = ivIninvoiceDao.out(startno, endno);
        PrintWriter out = response.getWriter();
        if (w == null) {
            ivout.setOutDate(new Date());
            ivout.setEmpID(EmpID);
            System.out.println(EmpID);
            ivout.setStartNo(startno);
            System.out.println(startno);
            ivout.setEndNo(endno);
            System.out.println(endno);
            ivout.setCounts(Integer.parseInt(request.getParameter("Counts")));
            int i = ivOutinvoiceDao.insertIvOutinvoice(ivout);
            if (i != 0) {
                System.out.println("成功");
            }
            out.write("true");
        } else {
            System.out.println("false");
        }
    }


    /**
     * 通过id查询IvOutinvoice数据的方法
     *
     * @return
     */
    @RequestMapping(value = "queryIvOutinvoiceById/{id}", produces = {"application/json;charset=UTF-8"})
    public String findOne(@PathVariable(name = "id") Object id, Map map) {
        IvOutinvoice obj = ivOutinvoiceService.findOne(id);
        map.put("IvOutinvoice", obj);
        return "页面名称自己改";
    }

    /**
     * 插入IvOutinvoice属性不为空的数据方法
     *
     * @return
     */
    @RequestMapping(value = "saveIvOutinvoice", produces = {"application/json;charset=UTF-8"})
    public String save(IvOutinvoice value) {
        ivOutinvoiceService.saveNotNull(value);
        return "redirect:/queryIvOutinvoice";
    }

    /**
     * 更新IvOutinvoice属性不为空的数据方法
     *
     * @return
     */
    @RequestMapping("addout")
    public ModelAndView addview(Model modle) {
        modle.addAttribute("date", new Date());
        return new ModelAndView("inv_invoiceOut.html");
    }

    @RequestMapping(value = "updateIvOutinvoice", produces = {"application/json;charset=UTF-8"})
    public String update(IvOutinvoice value) {
        ivOutinvoiceService.updateNotNullById(value);
        return "redirect:/queryIvOutinvoice";
    }

    /**
     * 通过id删除IvOutinvoice数据方法
     *
     * @return
     */
    @RequestMapping(value = "delIvOutinvoiceById/{id}", produces = {"application/json;charset=UTF-8"})
    public String delete(@PathVariable(name = "id") Object id) {
        ivOutinvoiceService.deleteById(id);
        return "redirect:/queryIvOutinvoice";
    }
}
